{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "29a152de",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-12T14:27:54.511886Z",
     "start_time": "2023-10-12T14:27:54.507302Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a99751ea",
   "metadata": {},
   "source": [
    "## baiscs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "892c8d9b",
   "metadata": {},
   "source": [
    "- `torch.triu`: upper\n",
    "- `torch.tril`: lower"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70372582",
   "metadata": {},
   "source": [
    "## cases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "58605127",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-12T14:27:11.134112Z",
     "start_time": "2023-10-12T14:27:11.125536Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf]], device='cuda:0')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq_len = 8\n",
    "# mask = torch.full((seq_len, seq_len), float('-inf'), device='cuda:0')\n",
    "mask = torch.full((seq_len, seq_len), float('-inf'), device='cuda:0')\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c86c43bf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-12T14:27:12.771374Z",
     "start_time": "2023-10-12T14:27:12.764122Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [0., -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [0., 0., -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [0., 0., 0., -inf, -inf, -inf, -inf, -inf],\n",
       "        [0., 0., 0., 0., -inf, -inf, -inf, -inf],\n",
       "        [0., 0., 0., 0., 0., -inf, -inf, -inf],\n",
       "        [0., 0., 0., 0., 0., 0., -inf, -inf],\n",
       "        [0., 0., 0., 0., 0., 0., 0., -inf]], device='cuda:0')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# mask = torch.triu(mask, diagonal=0)\n",
    "torch.triu(mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a6cec6aa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-12T14:27:59.967120Z",
     "start_time": "2023-10-12T14:27:59.958305Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [0., 0., 0., -inf, -inf, -inf, -inf, -inf],\n",
       "        [0., 0., 0., 0., -inf, -inf, -inf, -inf],\n",
       "        [0., 0., 0., 0., 0., -inf, -inf, -inf],\n",
       "        [0., 0., 0., 0., 0., 0., -inf, -inf],\n",
       "        [0., 0., 0., 0., 0., 0., 0., -inf],\n",
       "        [0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0., 0., 0., 0., 0.]], device='cuda:0')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(mask, diagonal=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "da9d38de",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-12T14:29:41.068374Z",
     "start_time": "2023-10-12T14:29:41.059374Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-inf, -inf, -inf, 0., 0., 0., 0., 0.],\n",
       "        [-inf, -inf, -inf, -inf, 0., 0., 0., 0.],\n",
       "        [-inf, -inf, -inf, -inf, -inf, 0., 0., 0.],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, 0., 0.],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, 0.],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],\n",
       "        [-inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf]], device='cuda:0')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tril(mask, diagonal=2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
